public class KnightTwo {
    int N = 5;  //棋盘规格
    int[][] map = new int[N][N];
    int TOTALLSTEPS = N * N;
    int[][] OldXY = new int[N * N + 1][2]; //用于存每一次跳的坐标点

    public KnightTwo(int x, int y) {
        move(x, y, TOTALLSTEPS);
    }

    public boolean move(int x, int y, int TOTALLSTEPS) {
        int step = 1;
        int n = 0;
        while (step <= TOTALLSTEPS) {
            if (map[x][y] == 0) {   //该点未被访问过 需要进行标记
                map[x][y] = step;
                OldXY[n][0] = x;    //记录该点坐标
                OldXY[n][1] = y;
                step++;
            }
            if (judge(x+1,y+2) == true) {
                x += 1;
                y += 2;
                n++;
                continue;
            }
            if (judge(x+1,y-2) == true) {
                x += 1;
                y -= 2;
                n++;
                continue;
            }
            if (judge(x-1,y+2) == true) {
                x -= 1;
                y += 2;
                n++;
                continue;
            }
            if (judge(x-1,y-2) == true) {
                x -= 1;
                y -= 2;
                n++;
                continue;
            }
            if (judge(x+2,y+1) == true) {
                x += 2;
                y += 1;
                n++;
                continue;
            }
            if (judge(x+2,y-1) == true) {
                x += 2;
                y -= 1;
                n++;
                continue;
            }
            if (judge(x-2,y+1) == true) {
                x -= 2;
                y += 1;
                n++;
                continue;
            }
            if (judge(x-2,y-1) == true) {
                x -= 2;
                y -= 1;
                n++;
                continue;
            } else {
                n--;
                x = OldXY[n][0];
                y = OldXY[n][1];
                continue;
            }
        }
        return true;
    }

    /**
     * 判断坐标是否越界 是否已经被访问
     * @param x
     * @param y
     * @return
     */
    public boolean judge(int x, int y) {
        if (x < 0 || x > N-1 || y < 0 || y > N-1)
            return false;
        if (map[x][y] != 0)
            return false;

        return true;
    }
}
